package com.qixiaobao.web.controller.direct.invoice;

import com.qixiaobao.common.annotation.Log;
import com.qixiaobao.common.annotation.RepeatSubmit;
import com.qixiaobao.common.core.domain.AjaxResult;
import com.qixiaobao.common.core.page.TableDataInfo;
import com.qixiaobao.common.enums.BusinessType;
import com.qixiaobao.common.exception.UtilException;
import com.qixiaobao.common.utils.easyExcel.EasyExcelUtil;
import com.qixiaobao.direct.invoice.domain.dto.DirectInvoiceDTO;
import com.qixiaobao.direct.invoice.domain.vo.DirectWaitInvoiceVO;
import com.qixiaobao.direct.invoice.service.IDirectInvoiceService;
import com.qixiaobao.web.controller.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 直投发票管理
 * @Author yangxm
 * @Date 2025/7/2 15:15
 */
@RestController
@RequestMapping("/direct/invoice")
public class DirectInvoiceController extends BaseController {

    @Autowired
    private IDirectInvoiceService directInvoiceService;
    
    /**
     * @Description 待开票列表
     * @Author yangxm
     * @Date 2025/7/2 15:23
     * @param directInvoiceDTO 
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/wait/list")
    public TableDataInfo waitList(DirectInvoiceDTO directInvoiceDTO){
        directInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        return getDataTable(directInvoiceService.selectWaitInvoiceList(directInvoiceDTO));
    }

    /**
     * @Description 待开票 - 开票金额合计
     * @Author yangxm
     * @Date 2025/7/3 9:42
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/wait/total")
    public AjaxResult waitTotal(DirectInvoiceDTO directInvoiceDTO){
        directInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        return success(directInvoiceService.selectWaitInvoiceTotal(directInvoiceDTO));
    }

    /**
     * @Description 待开票 - 导出
     * @Author yangxm
     * @Date 2025/7/3 9:33
     * @param directInvoiceDTO
     * @param response
     */
    @Log(title = "直投发票管理-待开票", businessType = BusinessType.EXPORT)
    @GetMapping("/wait/export")
    public void waitExport(DirectInvoiceDTO directInvoiceDTO, HttpServletResponse response){
        directInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        List<DirectWaitInvoiceVO> list = directInvoiceService.selectWaitInvoiceList(directInvoiceDTO);
        try {
            EasyExcelUtil.writeExcel(response, list, DirectWaitInvoiceVO.class, "直投发票管理-待开票", "sheet1");
        }catch (IOException e){
            throw new UtilException("导出失败：" + e.getMessage());
        }
    }

    /**
     * @Description 开票中、已开票 列表
     * @Author yangxm
     * @Date 2025/7/3 10:01
     * @param directInvoiceDTO 
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/list")
    public TableDataInfo invoiceList(DirectInvoiceDTO directInvoiceDTO){
        directInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        return getDataTable(directInvoiceService.selectInvoiceList(directInvoiceDTO));
    }

    /**
     * @Description 开票中、已开票 - 开票金额合计
     * @Author yangxm
     * @Date 2025/7/3 10:02
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/total")
    public AjaxResult invoiceTotal(DirectInvoiceDTO directInvoiceDTO){
        directInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        return success(directInvoiceService.selectInvoiceTotal(directInvoiceDTO));
    }

    /**
     * @Description 开票中、已开票 - 导出
     * @Author yangxm
     * @Date 2025/7/3 10:02
     * @param directInvoiceDTO
     * @param response
     */
    @Log(title = "直投发票管理-开票中、已开票", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void invoiceExport(DirectInvoiceDTO directInvoiceDTO, HttpServletResponse response){
        directInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        directInvoiceService.export(directInvoiceDTO, response);
    }

    /**
     * @Description 开票中 - 修改备注
     * @Author yangxm
     * @Date 2025/7/3 13:16
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "直投发票管理-开票中", businessType = BusinessType.UPDATE, businessKey = "备注")
    @PutMapping("/update/supplyRemark")
    public AjaxResult updateSupplyRemark(@RequestBody DirectInvoiceDTO directInvoiceDTO){
        return toAjax(directInvoiceService.updateSupplyRemark(directInvoiceDTO));
    }

    /**
     * @Description 开票中 - 开票
     * @Author yangxm
     * @Date 2025/7/3 13:35
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "直投发票管理-开票中", businessType = BusinessType.UPDATE, businessKey = "开票")
    @PostMapping("/uploadInvoiceFile")
    public AjaxResult uploadInvoiceFile(@RequestBody DirectInvoiceDTO directInvoiceDTO){
        return toAjax(directInvoiceService.uploadInvoiceFile(directInvoiceDTO));
    }

    /**
     * @Description 开票中、已开票 - 详情
     * @Author yangxm
     * @Date 2025/7/3 14:40
     * @param invoiceId
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/charge/list/{invoiceId}")
    public TableDataInfo chargeList(@PathVariable Long invoiceId){
        startPage();
        return getDataTable(directInvoiceService.selectInvoiceChargeList(invoiceId));
    }
}
